<script setup>
import { ref, defineEmits, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { addApi, updateApi } from '@/api/warehousingentry/warehousingEntryDetails'
import { getSkuInfoApi } from '@/api/item'
const emit = defineEmits(['refresh'])
const ruleFormRef = ref(null)
const rules = ref({
  skuId: [
    { required: true, message: '请输入规格ID', trigger: 'blur' }
  ],
  count: [
    { required: true, message: '请输入数量', trigger: 'blur' }
  ],
  supplierId: [
    { required: true, message: '请输入供应商ID', trigger: 'blur' }
  ],
  price: [
    { required: true, message: '请输入单价', trigger: 'blur' }
  ],
  warehousingEntryId: [
    { required: true, message: '请输入入库单ID', trigger: 'blur' }
  ],
  predictedArrivalTime: [
    { required: true, message: '请选择预测到达时间', trigger: 'blur' }
  ]
})

const dialogVisible = ref(false)
const dialogFormData = ref({
  id: null,
  skuId: null,
  count: 0,
  supplierId: null,
  price: 0,
  warehousingEntryId: null,
  realArrivalTime: '',
  predictedArrivalTime: '',
  unloadEmpId: null,
  unloadTime: '',
  status: 1,
  saasId: null,
  isDel: 1
})

const resetForm = () => {
  dialogVisible.value = false
  dialogFormData.value = {
    id: null,
    skuId: null,
    count: 0,
    supplierId: null,
    price: 0,
    warehousingEntryId: null,
    realArrivalTime: '',
    predictedArrivalTime: '',
    unloadEmpId: null,
    unloadTime: '',
    status: 1,
    saasId: null,
    isDel: 1
  }
  ruleFormRef.value?.resetFields()
}

const confirm = async () => {
  if (!ruleFormRef.value) return
  await ruleFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      let res = null
      if (dialogFormData.value.id) {
        res = await updateApi(dialogFormData.value)
      } else {
        res = await addApi(dialogFormData.value)
      }
      if (res.code) {
        ElMessage.success('操作成功!')
        resetForm()
        emit('refresh')
      } else {
        ElMessage.error('操作失败！')
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
const itemInfo = ref()
const getItemList = async () => {
  let res = await getSkuInfoApi()
  itemInfo.value = res
}
onMounted(() => {
  getItemList()
})
defineExpose({
  itemInfo,
  dialogVisible,
  dialogFormData
})
</script>

<template>
  <el-dialog v-model="dialogVisible" title="入库明细信息" width="600px">
    <el-form :model="dialogFormData" label-width="130px" ref="ruleFormRef" :rules="rules">
      <el-form-item label="规格ID" prop="skuId">
        <el-input v-model="dialogFormData.skuId" placeholder="请输入规格ID" />
      </el-form-item>

      <el-form-item label="数量" prop="count">
        <el-input-number v-model="dialogFormData.count" :min="0" />
      </el-form-item>

      <el-form-item label="供应商ID" prop="supplierId">
        <el-input v-model="dialogFormData.supplierId" placeholder="请输入供应商ID" />
      </el-form-item>

      <el-form-item label="单价(分)" prop="price">
        <el-input-number v-model="dialogFormData.price" :min="0" />
      </el-form-item>

      <el-form-item label="入库单ID" prop="warehousingEntryId">
        <el-input v-model="dialogFormData.warehousingEntryId" placeholder="请输入入库单ID" />
      </el-form-item>

      <el-form-item label="实际到达时间">
        <el-date-picker v-model="dialogFormData.realArrivalTime" type="datetime" placeholder="请选择实际到达时间"
          style="width: 100%" />
      </el-form-item>

      <el-form-item label="预测到达时间" prop="predictedArrivalTime">
        <el-date-picker v-model="dialogFormData.predictedArrivalTime" type="datetime" placeholder="请选择预测到达时间"
          style="width: 100%" />
      </el-form-item>

      <el-form-item label="卸货员工ID">
        <el-input v-model="dialogFormData.unloadEmpId" placeholder="请输入卸货员工ID" />
      </el-form-item>

      <el-form-item label="卸货时间">
        <el-date-picker v-model="dialogFormData.unloadTime" type="datetime" placeholder="请选择卸货时间" style="width: 100%" />
      </el-form-item>

      <el-form-item label="状态">
        <el-select v-model="dialogFormData.status" placeholder="请选择状态">
          <el-option label="未到货" :value="1" />
          <el-option label="已到货" :value="2" />
          <el-option label="已卸货" :value="3" />
          <el-option label="分拣中" :value="4" />
          <el-option label="完成分拣" :value="5" />
        </el-select>
      </el-form-item>

      <el-form-item label="是否有效">
        <el-switch v-model="dialogFormData.isDel" :active-value="1" :inactive-value="2" />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="resetForm">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>